package com.uenpay.util.adapter;

import com.bumptech.glide.Glide;
import com.uenpay.util.imageloader.transform.GlideCircleTransform;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by PS on 2017/3/21.
 */

public class CommonViewHolder extends RecyclerView.ViewHolder {

    //视图集合
    private SparseArray<View> mViews;
    private View mItemView;
    private View mConvertView;
    private Context mContext;
    private int mPosition;

    public CommonViewHolder(Context context, View itemView, int position) {
        super(itemView);
        mContext = context;
        mItemView = itemView;
        mPosition = position;
        mViews = new SparseArray<View>();
    }

    public static CommonViewHolder get(Context context, ViewGroup parent, int layoutId){
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        CommonViewHolder holder = new CommonViewHolder(context, itemView, -1);
        return holder;
    }

    public static CommonViewHolder get(Context context, ViewGroup parent, int layoutId, int position){
		if (parent == null) {

		}
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        CommonViewHolder holder = new CommonViewHolder(context, itemView, position);
        return holder;
    }

    /**
     *
     * 通过viewId 获取对应视图
     *
     * */
    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if (view == null){
            view = mItemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public CommonViewHolder setText(int viewId, String text){
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    public CommonViewHolder setImageResource(int viewId, int resId){
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    public CommonViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    public CommonViewHolder setImageUrl(int viewId, String imageUrl) {
        if ("".equals(imageUrl) || imageUrl == null) {
            return this;
        }

        ImageView view = getView(viewId);
        Glide.with(mContext).load(imageUrl).into(view);
        return this;
    }

    /**
     * @param viewId
     *            The view id.
     * @param imageUrl
     *            The image URL.
     * @param drawableRes
     *            没有加载完时 显示的默认图片
     *            默认圆角
     * @return The BaseAdapterHelper for chaining.
     */
    public CommonViewHolder setImageUrl(int viewId, String imageUrl,
                                      int drawableRes) {
        ImageView view = getView(viewId);
        if ("".equals(imageUrl) || imageUrl == null) {
            view.setImageResource(drawableRes);
            return this;
        }
        Glide.with(mContext).load(imageUrl).placeholder(drawableRes).transform(new GlideCircleTransform(mContext)).into(view);
        return this;
    }

    /**
     * @param viewId
     *            The view id.
     * @param imageUrl
     *            The image URL.
     * @param drawableRes
     *            没有加载完时 显示的默认图片
     * @param isCircle
     *            非圆角
     * @return The BaseAdapterHelper for chaining.
     */
    public CommonViewHolder setImageUrl(int viewId, String imageUrl, int drawableRes, boolean isCircle) {
        ImageView view = getView(viewId);
        if (!isCircle) {
            Glide.with(mContext).load(imageUrl).placeholder(drawableRes).into(view);
        }
        return this;
    }

    public CommonViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        if (null != bitmap) {
            ImageView view = getView(viewId);
            view.setImageBitmap(bitmap);
        }
        return this;
    }

    /**
     * Add an action to set the alpha of a view. Can be called multiple times.
     * Alpha between 0-1.
     */
    public CommonViewHolder setAlpha(int viewId, float value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getView(viewId).setAlpha(value);
        } else {
            // Pre-honeycomb hack to set Alpha value
            AlphaAnimation alpha = new AlphaAnimation(value, value);
            alpha.setDuration(0);
            alpha.setFillAfter(true);
            getView(viewId).startAnimation(alpha);
        }
        return this;
    }

    public CommonViewHolder setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public CommonViewHolder setBackgroundColor(int viewId, int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public CommonViewHolder setBackgroundRes(int viewId, int backgroundRes) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    public CommonViewHolder setTextColor(int viewId, int textColor) {
        TextView view = getView(viewId);
        view.setTextColor(textColor);
        return this;
    }

    public CommonViewHolder setTextColorRes(int viewId, int textColorRes) {
        TextView view = getView(viewId);
        view.setTextColor(mContext.getResources().getColor(textColorRes));
        return this;
    }

    public CommonViewHolder setOnClickListener(int viewId,
                                               View.OnClickListener listener){
        getView(viewId).setOnClickListener(listener);
        return this;
    }

    public CommonViewHolder setOnTouchListener(int viewId,
                                             View.OnTouchListener listener) {
        getView(viewId).setOnTouchListener(listener);
        return this;
    }

}
